<template>
  <div class="app-container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb-container">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <router-link to="/index">
            <i class="el-icon-s-home"></i> 首页
          </router-link>
        </el-breadcrumb-item>
        <el-breadcrumb-item>救援服务</el-breadcrumb-item>
        <el-breadcrumb-item>小修价格</el-breadcrumb-item>
        <el-breadcrumb-item>服务商价格配置</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="小修类型" prop="repairType">
        <el-select
          v-model="queryParams.repairType"
          placeholder="请选择小修类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.minor_repair_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option label="正常" value="0" />
          <el-option label="停用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="生效日期">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="priceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配置ID" align="center" prop="id" />
      <el-table-column label="小修类型" align="center" prop="repairType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.minor_repair_type" :value="scope.row.repairType"/>
        </template>
      </el-table-column>
      <el-table-column label="司机起步价" align="center" prop="driverStartingPrice">
        <template slot-scope="scope">
          <span>{{ scope.row.driverStartingPrice }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="司机免费里程" align="center" prop="driverFreeMileage">
        <template slot-scope="scope">
          <span>{{ scope.row.driverFreeMileage }}km</span>
        </template>
      </el-table-column>
      <el-table-column label="司机超出单价" align="center" prop="driverUnitPrice">
        <template slot-scope="scope">
          <span>{{ scope.row.driverUnitPrice }}元/km</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="生效日期" align="center" prop="effectiveDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效日期" align="center" prop="expiryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务商小修价格配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="60%"
      append-to-body
      custom-class="repair-dialog"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        class="repair-form"
      >
        <el-divider content-position="center">
          <span class="divider-title">基本信息</span>
        </el-divider>
        <div class="form-container">
          <el-row :gutter="30">
            <el-col :span="24">
              <el-form-item label="小修类型" prop="repairType">
                <el-select
                  v-model="form.repairType"
                  placeholder="请选择小修类型"
                  clearable
                  class="full-width-input"
                >
                  <el-option
                    v-for="dict in dict.type.minor_repair_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <el-divider content-position="center">
          <span class="divider-title">生效日期</span>
        </el-divider>
        <div class="form-container">
          <div class="date-section">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="生效日期" prop="effectiveDate">
                  <el-date-picker
                    v-model="form.effectiveDate"
                    type="date"
                    placeholder="选择生效日期"
                    value-format="yyyy-MM-dd"
                    class="full-width-input"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="失效日期" prop="expiryDate">
                  <el-date-picker
                    v-model="form.expiryDate"
                    type="date"
                    placeholder="选择失效日期"
                    value-format="yyyy-MM-dd"
                    class="full-width-input"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
          </div>

          <el-divider content-position="center">
            <span class="divider-title">价格配置</span>
          </el-divider>

          <!-- 司机价格模块 -->
          <div class="price-section">
            <div class="section-header driver-header">
              <i class="el-icon-s-custom"></i>
              <span>司机价格配置</span>
            </div>
            <div class="compact-form-items">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item
                    label="起步价格"
                    prop="driverStartingPrice"
                  >
                    <div class="input-with-unit">
                      <el-input-number
                        :precision="2"
                        :step="1"
                        :max="10000"
                        :min="0"
                        v-model="form.driverStartingPrice"
                        placeholder="请输入起步价格"
                        class="full-width-input"
                        controls-position="right"
                      ></el-input-number>
                      <span class="unit-suffix">元</span>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item
                    label="免费里程"
                    prop="driverFreeMileage"
                  >
                    <div class="input-with-unit">
                      <el-input-number
                        :precision="1"
                        :step="1"
                        :max="1000"
                        :min="0"
                        v-model="form.driverFreeMileage"
                        placeholder="请输入免费里程"
                        class="full-width-input"
                        controls-position="right"
                      ></el-input-number>
                      <span class="unit-suffix">km</span>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item
                    label="超出单价"
                    prop="driverUnitPrice"
                  >
                    <div class="input-with-unit">
                      <el-input-number
                        :precision="2"
                        :step="1"
                        :max="1000"
                        :min="0"
                        v-model="form.driverUnitPrice"
                        placeholder="请输入超出单价"
                        class="full-width-input"
                        controls-position="right"
                      ></el-input-number>
                      <span class="unit-suffix">元/km</span>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>

          <!-- 状态配置 -->
          <div class="price-section remark-section">
            <div class="section-header">
              <i class="el-icon-setting"></i>
              <span>状态配置</span>
            </div>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="0">正常</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" icon="el-icon-check"
          >确 定</el-button
        >
        <el-button @click="cancel" icon="el-icon-close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listServiceProviderRepairPrice,
  getServiceProviderRepairPrice,
  delServiceProviderRepairPrice,
  addServiceProviderRepairPrice,
  updateServiceProviderRepairPrice,
  exportServiceProviderRepairPrice
} from '@/api/repair/serviceProviderRepairPrice'

export default {
  name: 'ServiceProviderRepairPrice',
  dicts: ['sys_normal_disable', 'minor_repair_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务商小修价格配置表格数据
      priceList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairType: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        repairType: [
          { required: true, message: '小修类型不能为空', trigger: 'change' }
        ],
        driverStartingPrice: [
          { required: true, message: '司机起步价不能为空', trigger: 'blur' }
        ],
        driverFreeMileage: [
          { required: true, message: '司机免费里程不能为空', trigger: 'blur' }
        ],
        driverUnitPrice: [
          { required: true, message: '司机超出单价不能为空', trigger: 'blur' }
        ],
        effectiveDate: [
          { required: true, message: '生效日期不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询服务商小修价格配置列表 */
    getList() {
      this.loading = true
      const queryParams = { ...this.queryParams }
      if (this.dateRange && this.dateRange.length === 2) {
        queryParams.params = {
          beginTime: this.dateRange[0],
          endTime: this.dateRange[1]
        }
      }
      listServiceProviderRepairPrice(queryParams).then(response => {
        this.priceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        repairType: null,
        driverStartingPrice: null,
        driverFreeMileage: null,
        driverUnitPrice: null,
        effectiveDate: null,
        expiryDate: null,
        status: '0'
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加服务商小修价格配置'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getServiceProviderRepairPrice(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改服务商小修价格配置'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateServiceProviderRepairPrice(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addServiceProviderRepairPrice(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除服务商小修价格配置编号为"' + ids + '"的数据项？').then(function() {
        return delServiceProviderRepairPrice(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('api/serviceProviderRepairPrice/export', {
        ...this.queryParams
      }, `service_provider_repair_price_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>
.repair-dialog {
  border-radius: 8px;
}

.repair-form {
  padding: 0 15px;
}

.form-container {
  margin-bottom: 15px;
}

.price-section {
  background-color: #f9fafc;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.03);
  margin-bottom: 15px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.price-section:hover {
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.08);
}

.section-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  font-size: 16px;
  font-weight: 600;
  color: #ffffff;
  padding: 8px 15px;
  border-radius: 4px;
  background: linear-gradient(135deg, #409eff 0%, #64b5f6 100%);
}

.driver-header {
  background: linear-gradient(135deg, #e6a23c 0%, #f3d19e 100%);
}

.remark-section .section-header {
  background: linear-gradient(135deg, #909399 0%, #c0c4cc 100%);
}

.section-header i {
  margin-right: 8px;
  color: #ffffff;
  font-size: 18px;
}

.date-section {
  margin-bottom: 15px;
  background-color: #ffffff;
  padding: 10px;
  border-radius: 6px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.compact-form-items {
  background-color: #ffffff;
  padding: 12px;
  border-radius: 6px;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.03);
  margin-bottom: 8px;
}

.compact-form-items .el-form-item {
  margin-bottom: 10px;
}

.compact-form-items .el-form-item:last-child {
  margin-bottom: 0;
}

.divider-title {
  font-size: 18px;
  font-weight: bold;
  color: #409eff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.el-divider {
  margin: 20px 0;
}

.unit-suffix {
  display: inline-block;
  margin-left: 5px;
  width: 25px;
  color: #909399;
  font-size: 12px;
  line-height: 36px;
  white-space: nowrap;
  text-align: left;
}

.input-with-unit {
  display: flex;
  align-items: center;
  width: 100%;
}

.full-width-input {
  width: 100%;
}

.el-input-number.full-width-input.is-controls-right {
  padding-right: 0 !important;
  width: calc(100% - 30px) !important;
}

.dialog-footer {
  text-align: center;
  padding-top: 10px;
}

.breadcrumb-container {
  padding: 10px 0;
  margin-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
  background-color: #fff;
}

.breadcrumb-container .el-breadcrumb {
  font-size: 14px;
}

.breadcrumb-container i {
  margin-right: 5px;
  color: #409eff;
}

/** 搜索输入框样式优化 */
.el-form--inline .el-form-item {
  margin-right: 20px;
}

.el-form--inline .el-form-item .el-input {
  width: 200px;
}

.el-form--inline .el-form-item .el-select {
  width: 200px;
}

.el-form--inline .el-form-item .el-date-editor {
  width: 240px;
}
</style>
